package com.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.SysPrivilege;
import com.chen.dto.R;
import com.chen.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author: chen
 * @Date: 2021/4/20 9:16 AM
 * @Version 1.0
 */
@RestController
@RequestMapping("/privileges")
@Api(tags = "权限配置控制器")
public class SysPrivilegeController {

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    //分页查询权限配置列表
    @GetMapping
    @ApiOperation(value = "分页查询权限配置列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "current", value = "当前页数"),
                    @ApiImplicitParam(name = "size", value = "每页显示数量")
            }
    )
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> list(Page<SysPrivilege> privilegePage) {
        Page<SysPrivilege> page = sysPrivilegeService.page(privilegePage);
        return R.ok(page);
    }


    //新增系统权限
    @PostMapping
    @ApiOperation(value = "新增系统权限配置")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "description", value = "权限配置的描述"),
                    @ApiImplicitParam(name = "menuId", value = "菜单id"),
                    @ApiImplicitParam(name = "name", value = "权限配置的名称")
            }
    )
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R add(@ApiIgnore @RequestBody SysPrivilege sysPrivilege) {
        sysPrivilegeService.save(sysPrivilege);
        return R.ok();
    }

    //编辑系统权限
    @PatchMapping
    @ApiOperation(value = "修改系统权限配置【根据ID修改】")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "description", value = "权限配置的描述"),
                    @ApiImplicitParam(name = "id", value = "需要修改的配置记录"),
                    @ApiImplicitParam(name = "menuId", value = "菜单id"),
                    @ApiImplicitParam(name = "name", value = "权限配置的名称")
            }
    )
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R update(@ApiIgnore @RequestBody SysPrivilege sysPrivilege) {
        sysPrivilegeService.updateById(sysPrivilege);
        return R.ok();
    }

    //删除系统权限
}
